package com.zprmb.udzt;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.gesture.GestureEditActivity;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.VolleryUtil;

import org.json.JSONObject;

public class V6CheckPassWord extends BaseActivity implements OnClickListener{
	private EditText et_login_pass;
	private Button btn_confirm;
	private TextView tv_cancle;
	private LinearLayout li_opera;
	private ImageView pass;
	private ImageView img_clear;
	Intent intent=new Intent();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v6_check_password);
		et_login_pass=(EditText)findViewById(R.id.et_login_pass);
		btn_confirm=(Button)findViewById(R.id.btn_confirm);
		tv_cancle=(TextView)findViewById(R.id.tv_cancle);
		li_opera=(LinearLayout)findViewById(R.id.li_opera);
		img_clear=(ImageView)findViewById(R.id.img_clear);
		pass=(ImageView)findViewById(R.id.img_pass);
		pass.setOnClickListener(this);
		img_clear.setOnClickListener(this);	
		tv_cancle.setOnClickListener(this);
		btn_confirm.setOnClickListener(this);

//		Toast.makeText(this, "登录密码修改呢", Toast.LENGTH_SHORT).show();

		et_login_pass.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(s.length()>=1){
					btn_confirm.setEnabled(true);
					btn_confirm.setBackgroundResource(R.drawable.v6button);
				}else{
					btn_confirm.setEnabled(false);
					btn_confirm.setBackgroundResource(R.drawable.btn_unclick);
				}
				if(et_login_pass.getText().toString().equals("")){
					li_opera.setVisibility(View.GONE);
				}else{
					li_opera.setVisibility(View.VISIBLE);
				}
			}
		});
	}
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if(arg2!=null){
//			reload=arg1;
//			Log.e("arg1intent", arg2.getIntArrayExtra("resultcode")+"");
			LogUtils.e(V6CheckPassWord.this, arg1+"");
			if(arg1==0){
				
				setResult(2,intent);
				finish();
//				

			}
		}
	}
	private void V6login(){
		AjaxParams params=new AjaxParams();
		
		params.put("userMobile", getMobile());
		params.put("password", et_login_pass.getText().toString());
//		Log.e("mobile", params.toString());
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("login", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					
			
				
				if(getIntent().getIntExtra("open", 0)==1){
					   
					Intent intent = new Intent(V6CheckPassWord.this,GestureEditActivity.class);//MainActivity
					intent.putExtra("update", 1);
					intent.putExtra("from", "close");
					startActivityForResult(intent, 1);
					
				}else{
				Editor editor2=getShare().edit();
				editor2.putString(getMobile(),"");
				editor2.putInt("ge",1);
				editor2.commit();
				setResult(2,intent);
					finish();
				}
				
					
				
					
				
				
				
					
				
				}else {
					if (jsonObject.optString("comment").contains("账户暂被锁定")){

						showUpVersionDialog(jsonObject.optString("comment"));
						btn_confirm.setBackgroundResource(R.drawable.v6button);
						btn_confirm.setText("验证");
						btn_confirm.setEnabled(true);
//						Toast.makeText(V6CheckPassWord.this,jsonObject.optString("comment"), 0).show();

					}else {
						btn_confirm.setBackgroundResource(R.drawable.v6button);
						btn_confirm.setText("验证");
						btn_confirm.setEnabled(true);
						Toast.makeText(V6CheckPassWord.this,jsonObject.optString("comment"), 0).show();

					}




				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setRequestMethod(1);
		volleryUtil.setShow(true);
		volleryUtil.setHeaders("token", getToken());
;//		volleryUtil.setHeaders("appVersion", getAppVersion());
//		volleryUtil.setHeaders("client", "android");
//		volleryUtil.setHeaders("deviceToken", getdeviceId());
//		volleryUtil.setHeaders("mobileType", getDeviceType());
		volleryUtil.execute(V6CheckPassWord.this, Constant.URL+"/api/v6/login/checkLoginPWD?",params.toString() );
	}

	Dialog dialog;

	private void showUpVersionDialog(String content) {
		dialog = new Dialog(V6CheckPassWord.this, R.style.alert_dialog);
		dialog.setCancelable(false);

		Window window = dialog.getWindow();
		window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(
				R.color.float_transparent)));
		window.setGravity(Gravity.CENTER);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		window.setContentView(R.layout.dialog_01);

		dialog.show();
		TextView tv01 = (TextView) window.findViewById(R.id.dl_tv_01);//联系客服
		TextView text_status = (TextView) window.findViewById(R.id.text_status);

		TextView dl_tv_02 = (TextView) window.findViewById(R.id.dl_tv_02);

		if (content.indexOf("，") != -1) {

			text_status.setText(content.substring(content.indexOf("，") + 1, content.length()));
		}
		tv01.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {


//                popWindow.dismiss();
				if (dialog != null) {
					dialog.dismiss();
				}
				BaseHelper.showDialog2(V6CheckPassWord.this, "拨号", getResources().getString(R.string.tel), 1);
			}
		});
		dl_tv_02.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (dialog != null) {
					dialog.dismiss();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){     
		case R.id.tv_cancle:
			finish();
		
		break;
		
		case R.id.btn_confirm:
			V6login();
			break;
		case R.id.img_pass:
			if(et_login_pass.getTransformationMethod().equals(PasswordTransformationMethod  
                    .getInstance())){
				et_login_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				pass.setBackgroundResource(R.drawable.yan1);
			}else{
				et_login_pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
				pass.setBackgroundResource(R.drawable.yan2);
			}
			break;
		case R.id.img_clear:
			et_login_pass.setText("");
			break;
		
		
		}
	}
}
